/*   Copyright (C) 2011 Wolfgang Hankeln, Norma J. Wendel
 *
 *   This file is part of CDinFusion.
 *
 *   CDinFusion is free software: you can redistribute it and/or
 *   modify it under the terms of the GNU Lesser General Public 
 *   License in version 3 (LGPL3) as published by the Free Software
 *   Foundation version 3 of the License.
 *
 *   CDinFusion is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *   Lesser General Public License for more details.
 *
 *   You should have received a copy of the Lesser General Public
 *   License along with CDinFusion; see the file COPYING. If not,
 *   write to the Free Software Foundation, 59 Temple Place - Suite 330,
 *   Boston, MA 02111-1307, USA or see <http://www.gnu.org/licenses/>.
 *   
 *   If you have any questions, send an email to: megx@mpi-bremen.de.
 */
package org.cdinfusion.web.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.cdinfusion.data.GetInformation;
import org.cdinfusion.data.environment.Air;
import org.cdinfusion.data.environment.Biofilm;
import org.cdinfusion.data.environment.Host_associated;
import org.cdinfusion.data.environment.Human_associated;
import org.cdinfusion.data.environment.Human_gut;
import org.cdinfusion.data.environment.Human_oral;
import org.cdinfusion.data.environment.Human_skin;
import org.cdinfusion.data.environment.Human_vaginal;
import org.cdinfusion.data.environment.Miscellaneous;
import org.cdinfusion.data.environment.Plant_associated;
import org.cdinfusion.data.environment.Sediment;
import org.cdinfusion.data.environment.Soil;
import org.cdinfusion.data.environment.Wastewater;
import org.cdinfusion.data.environment.Water;
import org.cdinfusion.data.studytype.BA;
import org.cdinfusion.data.studytype.EU;
import org.cdinfusion.data.studytype.ME;
import org.cdinfusion.data.studytype.MIMARKS_C;
import org.cdinfusion.data.studytype.MIMARKS_S;
import org.cdinfusion.data.studytype.ORG;
import org.cdinfusion.data.studytype.PL;
import org.cdinfusion.data.studytype.VI;
import org.cdinfusion.data.util.GSCCache;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class GSCCacheController extends AbstractController {
	public ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		System.out.println("##### GSC CACHE CONTROLLER #####");
		
		System.out.println("Clearing in-memory cache");
		GSCCache.clear();
		
		getLongnamesForStudyTypes();
		getLongnamesForEnvironments();

		System.out.println("Persisting cache");
		GSCCache.persist();

		return new ModelAndView("Cache");
	}

	private void getLongnamesForStudyTypes() throws IOException {
		
		System.out.println("Retrieving long names for study types");
		final long start = System.currentTimeMillis();

		GetInformation.nameInformation(new BA(), false);
		GetInformation.nameInformation(new EU(), false);
		GetInformation.nameInformation(new ME(), false);
		GetInformation.nameInformation(new MIMARKS_C(), false);
		GetInformation.nameInformation(new MIMARKS_S(), false);
		GetInformation.nameInformation(new ORG(), false);
		GetInformation.nameInformation(new PL(), false);
		GetInformation.nameInformation(new VI(), false);
		
		final long end = System.currentTimeMillis();
		System.out.println("done in " + (end-start) + "ms");
	}

	private void getLongnamesForEnvironments() throws IOException {

		System.out.println("Retrieving long names for environments");
		final long start = System.currentTimeMillis();

		
		GetInformation.nameInformation(new Air(), false);
		GetInformation.nameInformation(new Biofilm(), false);
		GetInformation.nameInformation(new Host_associated(), false);
		GetInformation.nameInformation(new Human_associated(), false);
		GetInformation.nameInformation(new Human_gut(), false);
		GetInformation.nameInformation(new Human_oral(), false);
		GetInformation.nameInformation(new Human_skin(), false);
		GetInformation.nameInformation(new Human_vaginal(), false);
		GetInformation.nameInformation(new Miscellaneous(), false);
		GetInformation.nameInformation(new Plant_associated(), false);
		GetInformation.nameInformation(new Sediment(), false);
		GetInformation.nameInformation(new Soil(), false);
		GetInformation.nameInformation(new Wastewater(), false);
		GetInformation.nameInformation(new Water(), false);
		
		final long end = System.currentTimeMillis();
		System.out.println("done in " + (end-start) + "ms");
	}
}